﻿#include "recipeloader.h"
#include <opencv2/highgui.hpp>
#include <QMessageBox>
#include <QDebug>
#include <QDir>

const int BORDER = 10;
RecipeLoader::RecipeLoader()
{

}

bool RecipeLoader::loadRecipe_Left(const QSettings &settings, const cv::Mat &image, PanelLocator &locator)
{
    locator.m_templateFull = image.clone();
    if(locator.m_templateFull.empty())
    {
        locator.m_valid = false;
        qDebug() << "locator.m_templateFull.empty()";
        return false;
    }
    int x1, y1, x2, y2;
    x1 = settings.value("LeftPanel/TopLeft_X", 0).toInt(); // 用来标志左边屏的位置。左上角和右下角
    y1 = settings.value("LeftPanel/TopLeft_Y", 0).toInt();
    x2 = settings.value("LeftPanel/BottomRight_X", 0).toInt();
    y2 = settings.value("LeftPanel/BottomRight_Y", 0).toInt();

    if(x1 == x2) return false;
    locator.m_threshold = settings.value("LeftPanel/Threshold", 0.65).toDouble();// 模板匹配超过这个值才认为找到Panel了
    locator.m_panelRect = QRect(QPoint(x1, y1), QPoint(x2,y2));

    qDebug() << "LeftPanel XRange: " << x1 << ", " << x2 << ", YRange: " << y1 << ", " << y2;
    locator.m_template = locator.m_templateFull(cv::Range(y1 - BORDER, y2 + BORDER), cv::Range(x1 - BORDER, x2 + BORDER));
    locator.m_panelTopLeft = cv::Point(x1 - BORDER, y1 - BORDER);// 实际的Panel模板比 Panel 略大一些

    x1 = settings.value("LeftPanel/SearchRegionTopLeft_X", 0).toInt();
    y1 = settings.value("LeftPanel/SearchRegionTopLeft_Y", 0).toInt();
    x2 = settings.value("LeftPanel/SearchRegionBottomRight_X", 0).toInt();
    y2 = settings.value("LeftPanel/SearchRegionBottomRight_Y", 0).toInt();
    locator.m_searchRegion = cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)); // 模板匹配的搜索区域，用于加快搜索速度
    qDebug() << "LeftPanel/SearchRegion XRange: " << x1 << ", " << x2 << ", YRange: " << y1 << ", " << y2;

    locator.m_panelSize = locator.panelSize(image);
    qDebug() << "panelSize = " << locator.m_panelSize.width << ", " << locator.m_panelSize.height;

    x1 = settings.value("LeftPanel/EdgeTopLeft_X", 0).toInt();
    y1 = settings.value("LeftPanel/EdgeTopLeft_Y", 0).toInt();
    x2 = settings.value("LeftPanel/EdgeBottomRight_X", 0).toInt();
    y2 = settings.value("LeftPanel/EdgeBottomRight_Y", 0).toInt();
    locator.m_lineSearchRegion = cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2));
    qDebug() << "LeftPanel/LineSearchRegion XRange: " << x1 << ", " << x2 << ", YRange: " << y1 << ", " << y2;
    locator.m_valid = true;
    return true;
}

bool RecipeLoader::loadRecipe_Left(QString path, QString recipeName, PanelLocator &locator)
{
    locator.m_recipeName = recipeName;
    QString fileName = path + "/" + recipeName + ".bmp";
    cv::Mat garyImage = cv::imread(fileName.toLocal8Bit().data(), cv::IMREAD_GRAYSCALE);

    if(garyImage.empty()) // 没有模板文件
    {
        qCritical() << "m_template is empty";
        locator.m_valid = false;
        return false;
    }
    fileName = path + "/" + recipeName + ".ini";
    QDir dir(fileName);
    if(!dir.exists(fileName))  // 没有配置文件
    {
        qCritical() << "recipe file not exists: " << fileName;
        locator.m_valid = false;
        return false;
    }
    qDebug() << "recipe ini file path = " << fileName;
    QSettings settings( fileName, QSettings::IniFormat );
    return loadRecipe_Left(settings, garyImage, locator);
}

bool RecipeLoader::loadRecipe_Right(const QSettings &settings, const cv::Mat &image, PanelLocator &locator)
{
    locator.m_valid = false;
    locator.m_templateFull = image.clone();
    if(locator.m_templateFull.empty())
    {
        return false;
    }
    int x1, y1, x2, y2;
    x1 = settings.value("RightPanel/TopLeft_X", 0).toInt();
    y1 = settings.value("RightPanel/TopLeft_Y", 0).toInt();
    x2 = settings.value("RightPanel/BottomRight_X", 0).toInt();
    y2 = settings.value("RightPanel/BottomRight_Y", 0).toInt();
    if(x1 != x2)
    {
        locator.m_threshold = settings.value("RightPanel/Threshold", 0.65).toDouble();// 模板匹配超过这个值才认为找到Panel了
        locator.m_panelRect = QRect(QPoint(x1, y1), QPoint(x2, y2));
        locator. m_template = locator.m_templateFull(cv::Range(y1 - BORDER, y2 + BORDER), cv::Range(x1 - BORDER, x2 + BORDER));
        locator.m_panelTopLeft = cv::Point(x1 - BORDER, y1 - BORDER);

        x1 = settings.value("RightPanel/SearchRegionTopLeft_X", 0).toInt();
        y1 = settings.value("RightPanel/SearchRegionTopLeft_Y", 0).toInt();
        x2 = settings.value("RightPanel/SearchRegionBottomRight_X", 0).toInt();
        y2 = settings.value("RightPanel/SearchRegionBottomRight_Y", 0).toInt();
        locator.m_searchRegion = cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2));

        locator.m_panelSize = locator.panelSize(image);

        x1 = settings.value("RightPanel/EdgeTopLeft_X", 0).toInt();
        y1 = settings.value("RightPanel/EdgeTopLeft_Y", 0).toInt();
        x2 = settings.value("RightPanel/EdgeBottomRight_X", 0).toInt();
        y2 = settings.value("RightPanel/EdgeBottomRight_Y", 0).toInt();
        locator.m_lineSearchRegion = cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2));
        locator.m_valid = true;
    }
    return true;
}

bool RecipeLoader::loadRecipe_Right(QString path, QString recipeName, PanelLocator &locator)
{
    locator.m_recipeName = recipeName;
    QString fileName = path + "/" + recipeName + ".bmp";
    cv::Mat image = cv::imread(fileName.toLocal8Bit().data(), cv::IMREAD_GRAYSCALE);

    if(image.empty()) // 没有模板文件
    {
        qCritical() << "m_template is empty";
        locator.m_valid = false;
        return false;
    }
    fileName = path + "/" + recipeName + ".ini";
    QDir dir(fileName);
    if(!dir.exists(fileName))  // 没有配置文件
    {
        qCritical() << "recipe file not exists: " << fileName;
        locator.m_valid = false;
        return false;
    }
    qDebug() << "recipe ini file path = " << fileName;
    QSettings settings( fileName, QSettings::IniFormat );
    return loadRecipe_Right(settings, image, locator);
}
